home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Shareware City / Developers / src / kant-generator-04-c / Kant ƒ / Kode ƒ / kant main window.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-23  |  12.6 KB  |  536 lines  |  [TEXT/MMCC]

  1. #include "kant main window.h"
  2. #include "kant text twiddling.h"
  3. #include "kant parser.h"
  4. //#include "kant search.h"
  5. #include "buttons.h"
  6. #include "environment.h"
  7. #include "menus.h"
  8. #include "popup.h"
  9. #include "util.h"
  10. #include "main.h"
  11. #include "program globals.h"
  12.  
  13. /* internal procedures for kant main window.c only */
  14. static    void SetupTheMainWindow(WindowDataHandle theData);
  15. static    void ShutDownTheMainWindow(WindowDataHandle theData);
  16. static    void InitializeTheMainWindow(WindowDataHandle theData);
  17. static    void OpenTheMainWindow(WindowDataHandle theData);
  18. static    void IdleInMainWindow(WindowDataHandle theData);
  19. static    void KeyPressedInMainWindow(WindowDataHandle theData, unsigned char theChar);
  20. static    void MouseClickedInMainWindow(WindowDataHandle theData, Point thePoint);
  21. static    void DisposeTheMainWindow(WindowDataHandle theData);
  22. static    void ActivateTheMainWindow(WindowDataHandle theData);
  23. static    void DeactivateTheMainWindow(WindowDataHandle theData);
  24. static    void CopybitsTheMainWindow(WindowDataHandle theData);
  25. static    void DrawTheMainWindow(WindowDataHandle theData, short theDepth);
  26. static    void PutDataIntoTEFields(WindowDataHandle theData);
  27. static    void ResizeControlsInMainWindow(WindowDataHandle theData);
  28. static    void UpdateTheUpdateRgn(WindowDataHandle theData);
  29.  
  30. enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
  31. enum { key_PageUp=0x0b, key_PageDown };
  32. enum { key_Home=0x01 };
  33. enum { key_End=0x04 };
  34.  
  35. #define HEADER_SPACE        -1
  36. #define FOOTER_SPACE        5
  37. #define DEAD_SPACE_LEFT        5
  38. #define DEAD_SPACE_RIGHT    DEAD_SPACE_LEFT
  39. #define WINDOW_WIDTH        500
  40. #define    WINDOW_HEIGHT        292
  41. #define kGrowBoxSize        15
  42.  
  43. enum
  44. {
  45.     kMainWindowTE=0
  46. };
  47.  
  48. #define NUM_TE                1        /* number of editable boxes */
  49.  
  50. static    short            gOldForegroundTime;        /* stored foreground wait time */
  51. static    short            gWhichTE;                /* which edit box we're in now */
  52. static    Rect            gDestRect[NUM_TE], gViewRect[NUM_TE];
  53. ControlHandle            gVScrollBar, gHScrollBar;
  54. static    Rect            gVScrollBarRect, gHScrollBarRect;
  55. static    short            gRawBottom;
  56. static    CursHandle        gMyIBeamHandle;
  57. static    Boolean            gIsActive;
  58.  
  59. short MainWindowDispatch(WindowDataHandle theData, short theMessage,
  60.     unsigned long misc)
  61. {
  62.     short            theDepth;
  63.     unsigned char    theChar;
  64.     Point            thePoint;
  65.     
  66.     switch (theMessage)
  67.     {
  68.         case kNull:
  69.             IdleInMainWindow(theData);
  70.             return kSuccess;
  71.             break;
  72.         case kUpdate:
  73.             theDepth=misc&0x7fff;
  74.             DrawTheMainWindow(theData, theDepth);
  75.             return kSuccess;
  76.             break;
  77.         case kOpen:
  78.             OpenTheMainWindow(theData);
  79.             return kSuccess;
  80.             break;
  81.         case kKeydown:
  82.             theChar=misc&charCodeMask;
  83.             KeyPressedInMainWindow(theData, theChar);
  84.             return kSuccess;
  85.             break;
  86.         case kMousedown:
  87.              thePoint.h=(misc>>16)&0x7fff;
  88.              thePoint.v=misc&0x7fff;
  89.              MouseClickedInMainWindow(theData, thePoint);
  90.              return kSuccess;
  91.              break;
  92.          case kActivate:
  93.              ActivateTheMainWindow(theData);
  94.              return kSuccess;
  95.              break;
  96.          case kDeactivate:
  97.              DeactivateTheMainWindow(theData);
  98.              return kSuccess;
  99.              break;
  100.          case kGrow:
  101.          case kZoom:
  102.              ResizeControlsInMainWindow(theData);
  103.              return kFailure;    /* still want to do the default processing -- see main.c */
  104.              break;
  105.          case kGetGrowSize:
  106.              SetRect((Rect*)misc, 200, 48+kGrowBoxSize+1, 32766, 32767);
  107.              return kSuccess;
  108.              break;
  109.          case kInitialize:
  110.              InitializeTheMainWindow(theData);
  111.              return kSuccess;
  112.              break;
  113.         case kStartup:
  114.             SetupTheMainWindow(theData);
  115.             return kSuccess;
  116.             break;
  117.         case kDispose:
  118.             DisposeTheMainWindow(theData);
  119.             return kSuccess;
  120.             break;
  121.         case kShutdown:
  122.             ShutDownTheMainWindow(theData);
  123.             return kSuccess;
  124.             break;
  125.         case kCut:
  126.         case kCopy:
  127.         case kPaste:
  128.         case kClear:
  129.         case kSelectAll:
  130.             DealWithEditMenu(theData, theMessage);
  131.             return kSuccess;
  132.             break;
  133.         case kCopybits:
  134.             CopybitsTheMainWindow(theData);
  135.             return kSuccess;
  136.             break;
  137.     }
  138.     
  139.     return kFailure;
  140. }
  141.  
  142. void SetupTheMainWindow(WindowDataHandle theData)
  143. {
  144.     unsigned char    *titleStr="\puntitled";
  145.     
  146.     (**theData).windowHeight=qd.screenBits.bounds.bottom-qd.screenBits.bounds.top-LMGetMBarHeight()-28;
  147.     (**theData).windowWidth=qd.screenBits.bounds.right-qd.screenBits.bounds.left-70;
  148.     (**theData).windowType=zoomDocProc;
  149.     (**theData).windowBounds.top=qd.screenBits.bounds.top+LMGetMBarHeight()+20;
  150.     (**theData).windowBounds.left=qd.screenBits.bounds.left+10;
  151.     (**theData).hasCloseBox=TRUE;
  152.     (**theData).maxDepth=1;
  153.     Mymemcpy((**theData).windowTitle, titleStr, titleStr[0]+1);
  154.     gRawBottom=(**theData).windowHeight+1-kGrowBoxSize;
  155.     gMyIBeamHandle=GetCursor(iBeamCursor);
  156. }
  157.  
  158. void InitializeTheMainWindow(WindowDataHandle theData)
  159. {
  160.     (**theData).initialTopLeft.v=qd.screenBits.bounds.top+LMGetMBarHeight()+20;
  161.     (**theData).initialTopLeft.h=qd.screenBits.bounds.left+10;
  162. }
  163.  
  164. void ShutDownTheMainWindow(WindowDataHandle theData)
  165. {
  166.     ReleaseResource((Handle)gMyIBeamHandle);
  167. }
  168.  
  169. void OpenTheMainWindow(WindowDataHandle theData)
  170. {
  171.     TEHandle        hTE;
  172.     FontInfo        theFontInfo;
  173.     
  174.     SetRect(&gVScrollBarRect, (**theData).windowWidth-kGrowBoxSize, HEADER_SPACE,
  175.         (**theData).windowWidth+1, gRawBottom);
  176.     SetRect(&gHScrollBarRect, -1, (**theData).windowHeight-kGrowBoxSize,
  177.         (**theData).windowWidth-kGrowBoxSize+1, (**theData).windowHeight+1);
  178.     gVScrollBar=NewControl(GetWindowPtr(theData), &gVScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0);
  179.     gHScrollBar=NewControl(GetWindowPtr(theData), &gHScrollBarRect, "\p", TRUE, 0, 0, 0, scrollBarProc, 0);
  180.     
  181.     hTE=GetWindowData_hTE(theData, kMainWindowTE);
  182.     if (hTE==0L)
  183.     {
  184.         GetTERect(GetWindowPtr(theData), &gDestRect[kMainWindowTE]);
  185.         gViewRect[kMainWindowTE]=gDestRect[kMainWindowTE];
  186.         hTE=(**theData).hTE[kMainWindowTE]=TENew(&(gDestRect[kMainWindowTE]), &(gViewRect[kMainWindowTE]));
  187.         TextFont(monaco);
  188.         TextSize(9);
  189.         TextFace(0);
  190.         GetFontInfo(&theFontInfo);
  191.         TextFont(0);
  192.         TextSize(12);
  193.         TextFace(0);
  194.         (**hTE).txFont=monaco;
  195.         (**hTE).txSize=9;
  196.         (**hTE).txFace=0;
  197.         (**hTE).fontAscent=theFontInfo.ascent;
  198.         (**hTE).lineHeight=theFontInfo.ascent+theFontInfo.descent+theFontInfo.leading;
  199.         AdjustViewRect(hTE);
  200.         gViewRect[kMainWindowTE]=(**hTE).viewRect;
  201.         
  202.         TEAutoView(TRUE, hTE);
  203.         TESetClickLoop((ProcPtr)MyClikLoop, hTE);
  204.         
  205.         gWhichTE=kMainWindowTE;
  206.         PutDataIntoTEFields(theData);
  207.     }
  208.     
  209.     gIsActive=TRUE;
  210.     AdjustVScrollBar(gVScrollBar, hTE);
  211.     (**theData).offscreenNeedsUpdate=TRUE;
  212.     AdjustMenus();
  213.     DrawMenuBar();
  214. }
  215.  
  216. void PutDataIntoTEFields(WindowDataHandle theData)
  217. {
  218.     TEHandle        hTE;
  219.     
  220.     hTE=GetWindowData_hTE(theData, kMainWindowTE);
  221.     TESetSelect(0, 0, hTE);
  222.     TEKey(0x00, hTE);
  223.     TEKey(0x08, hTE);
  224. }
  225.  
  226. void IdleInMainWindow(WindowDataHandle theData)
  227. {
  228.     Point            thePoint;
  229.     
  230.     if (gInProgress)
  231.         return;
  232.     
  233.     if (gWhichTE==kMainWindowTE)
  234.         TEIdle((**theData).hTE[kMainWindowTE]);
  235.     SetPort(GetWindowPtr(theData));
  236.     GetMouse(&thePoint);
  237.     if (PtInRect(thePoint, &(**GetWindowData_hTE(theData, kMainWindowTE)).viewRect))
  238.     {
  239.         if (!gCustomCursor)
  240.         {
  241.             SetCursor(*gMyIBeamHandle);
  242.             gCustomCursor=TRUE;
  243.         }
  244.     }
  245.     else
  246.     {
  247.         gCustomCursor=FALSE;
  248.     }
  249. }
  250.  
  251. void KeyPressedInMainWindow(WindowDataHandle theData, unsigned char theChar)
  252. {
  253.     TEHandle        hTE;
  254.     
  255.     if (gInProgress)
  256.         return;
  257.     
  258.     hTE=GetWindowData_hTE(theData, gWhichTE);
  259.     switch (theChar)
  260.     {
  261.         case key_PageUp:
  262.             RawActionProc(gVScrollBar, inPageUp);
  263.             break;
  264.         case key_PageDown:
  265.             RawActionProc(gVScrollBar, inPageDown);
  266.             break;
  267.         case key_Home:
  268.             TEPinScroll(0, TEGetHeight((**hTE).nLines, 1, hTE), hTE);
  269.             break;
  270.         case key_End:
  271.             TEPinScroll(0, -TEGetHeight((**hTE).nLines, 1, hTE), hTE);
  272.             break;
  273.         default:
  274.             if (gWhichTE!=kMainWindowTE)
  275.             {
  276.                 TEDeactivate((**theData).hTE[gWhichTE]);
  277.                 gWhichTE=kMainWindowTE;
  278.                 TEActivate((**theData).hTE[gWhichTE]);
  279.             }
  280.             TEKey(theChar, hTE);
  281.             TESelView(hTE);
  282.             AdjustForEndScroll(gVScrollBar, hTE);
  283. //            SetLastFindPosition((**hTE).selEnd);
  284.             break;
  285.     }
  286.     
  287.     AdjustVScrollBar(gVScrollBar, hTE);
  288. }
  289.  
  290. void MouseClickedInMainWindow(WindowDataHandle theData, Point thePoint)
  291. {
  292.     short            i;
  293.     Boolean            gotone;
  294.     short            partCode;
  295.     ControlHandle    theControl;
  296.     short            scrollDistance;
  297.     short            oldSetting;
  298.     ControlActionUPP    rawActionUPP=NewControlActionProc(RawActionProc);
  299.     TEHandle        hTE;
  300.     
  301.     if (gInProgress)
  302.         return;
  303.     
  304.     gotone=FALSE;
  305.     if (!gotone)
  306.     {
  307.         for (i=0; ((i<NUM_TE) && (!gotone)); i++)
  308.         {
  309.             if (PtInRect(thePoint, &(gDestRect[i])))
  310.             {
  311.                 if (i!=gWhichTE)
  312.                 {
  313.                     TEDeactivate((**theData).hTE[gWhichTE]);
  314.                     gWhichTE=i;
  315.                     TEActivate((**theData).hTE[gWhichTE]);
  316.                 }
  317.                 
  318.                 if (gWhichTE==kMainWindowTE)
  319.                 {
  320.                     short            oldStart, oldEnd;
  321.                     
  322.                     hTE=GetWindowData_hTE(theData, kMainWindowTE);
  323.                     oldStart=(**hTE).selStart;
  324.                     oldEnd=(**hTE).selEnd;
  325.                     TEClick(thePoint, (GetTheModifiers()&512) ? TRUE : FALSE, (**theData).hTE[i]);
  326. //                    if (((**hTE).selStart!=oldStart) || ((**hTE).selEnd!=oldEnd))
  327. //                        SetRefreshOutput(TRUE);
  328. //                    SetLastFindPosition((**hTE).selEnd);
  329.                 }
  330.                 
  331.                 gotone=TRUE;
  332.             }
  333.         }
  334.     }
  335.     
  336.     if (!gotone)
  337.     {
  338.         partCode=FindControl(thePoint, GetWindowPtr(theData), &theControl);
  339.         if (theControl==gVScrollBar)
  340.         {
  341.             switch (partCode)
  342.             {
  343.                 case inThumb:
  344.                     oldSetting=GetControlValue(theControl);
  345.                     partCode=TrackControl(theControl, thePoint, 0L);
  346.                     if (partCode==inThumb)
  347.                     {
  348.                         scrollDistance=oldSetting-GetControlValue(theControl);
  349.                         if (scrollDistance!=0)
  350.                         {
  351.                             hTE=GetWindowData_hTE(theData, kMainWindowTE);
  352.                             TEPinScroll(0, scrollDistance, hTE);
  353.                         }
  354.                     }
  355.                     gotone=TRUE;
  356.                     break;
  357.                 case inUpButton:
  358.                 case inDownButton:
  359.                 case inPageUp:
  360.                 case inPageDown:
  361.                     partCode=TrackControl(theControl, thePoint, rawActionUPP);
  362.                     gotone=TRUE;
  363.                     break;
  364.             }
  365.         }
  366.     }
  367. }
  368.  
  369. void DisposeTheMainWindow(WindowDataHandle theData)
  370. {
  371.     short            i;
  372.     
  373.     for (i=0; i<NUM_TE; i++)
  374.     {
  375.         if ((**theData).hTE[i]!=0L)
  376.             TEDispose((**theData).hTE[i]);
  377.         (**theData).hTE[i]=0L;
  378.     }
  379. }
  380.  
  381. void ActivateTheMainWindow(WindowDataHandle theData)
  382. {
  383.     gOldForegroundTime=gForegroundWaitTime;
  384.     gForegroundWaitTime=0;
  385.     TEActivate((**theData).hTE[gWhichTE]);
  386.     gIsActive=TRUE;
  387. }
  388.  
  389. void DeactivateTheMainWindow(WindowDataHandle theData)
  390. {
  391.     gForegroundWaitTime=gOldForegroundTime;
  392.     TEDeactivate((**theData).hTE[gWhichTE]);
  393.     gIsActive=FALSE;
  394.     gCustomCursor=FALSE;
  395. }
  396.  
  397. void DrawTheMainWindow(WindowDataHandle theData, short theDepth)
  398. {
  399.     GrafPtr            curPort;
  400.     
  401. #if 0
  402.     if (theDepth>2)
  403.     {
  404.         GetForeColor(&oldForeColor);
  405.         GetBackColor(&oldBackColor);
  406.     }
  407. #endif
  408.     
  409.     GetPort(&curPort);
  410.     EraseRect(&(curPort->portRect));
  411.     
  412. #if 0
  413.     if (theDepth>2)
  414.     {
  415.         RGBForeColor(&oldForeColor);
  416.         RGBBackColor(&oldBackColor);
  417.     }
  418. #endif
  419. }
  420.  
  421. void CopybitsTheMainWindow(WindowDataHandle theData)
  422. {
  423.     Rect            tempRect;
  424.     GrafPtr            curPort;
  425.     
  426.     if (gIsActive)
  427.     {
  428.         DrawGrowIcon(GetWindowPtr(theData));
  429.     }
  430.     else
  431.     {
  432.         GetPort(&curPort);
  433.         
  434.         tempRect.bottom=curPort->portRect.bottom;
  435.         tempRect.right=curPort->portRect.right;
  436.         tempRect.left=tempRect.right-kGrowBoxSize+1;
  437.         tempRect.top=tempRect.bottom-kGrowBoxSize+1;
  438.         EraseRect(&tempRect);
  439.     }
  440.     
  441.     UpdateControls(GetWindowPtr(theData), GetWindowPtr(theData)->visRgn);
  442.     
  443.     tempRect=GetWindowPtr(theData)->portRect;
  444.     tempRect.bottom-=kGrowBoxSize;
  445.     tempRect.right-=kGrowBoxSize;
  446.     CopyBits(    &(GetOffscreenPtrFunction(theData)->portBits),
  447.                 &(GetWindowPtr(theData)->portBits),
  448.                 &tempRect, &tempRect, 0, 0L);
  449. }
  450.  
  451. void ResizeControlsInMainWindow(WindowDataHandle theData)
  452. {
  453.     TEHandle        hTE;
  454.     
  455.     AdjustScrollSizes(GetWindowPtr(theData), hTE=GetTheTextHandle(), gVScrollBar, gHScrollBar);
  456.     AdjustViewRect(hTE);
  457.     TECalText(hTE);
  458.     AdjustForEndScroll(gVScrollBar, hTE);
  459.     gDestRect[kMainWindowTE]=(**hTE).destRect;
  460.     gViewRect[kMainWindowTE]=(**hTE).viewRect;
  461.     AdjustVScrollBar(gVScrollBar, hTE);
  462. }
  463.  
  464. void DealWithEditMenu(WindowDataHandle theData, short theMessage)
  465. {
  466.     Handle            scrapHandle;
  467.     long            dummy;
  468.     unsigned long    scrapLength;
  469.     TEHandle        hTE;
  470.     
  471.     hTE=GetWindowData_hTE(theData, gWhichTE);
  472.     
  473.     switch (theMessage)
  474.     {
  475.         case kCut:
  476.             TECut(hTE);
  477.             ZeroScrap();
  478.             TEToScrap();
  479.             AdjustForEndScroll(gVScrollBar, hTE);
  480.             break;
  481.         case kCopy:
  482.             TECopy(hTE);
  483.             ZeroScrap();
  484.             TEToScrap();
  485.             break;
  486.         case kPaste:
  487.             scrapHandle=NewHandle(0L);
  488.             if (GetScrap(scrapHandle, 'TEXT', &dummy)!=noTypeErr)
  489.             {
  490.                 scrapLength=GetHandleSize(scrapHandle);
  491.                 TEFromScrap();
  492.                 TEPaste(hTE);
  493.             }
  494.             DisposeHandle(scrapHandle);
  495.             break;
  496.         case kClear:
  497.             TEDelete(hTE);
  498.             AdjustForEndScroll(gVScrollBar, hTE);
  499.             break;
  500.         case kSelectAll:
  501.             TESetSelect(0, 32767, hTE);
  502.             break;
  503.     }
  504.     
  505.     TESelView(hTE);
  506.     AdjustVScrollBar(gVScrollBar, hTE);
  507. }
  508.  
  509. enum ErrorType ParserDispatch(WindowDataHandle theData)
  510. {
  511.     enum ErrorType    parseError;
  512.     TEHandle        rawTE;
  513.     
  514.     gCustomCursor=FALSE;
  515.     gInProgress=TRUE;
  516.     AdjustMenus();
  517.     DrawMenuBar();
  518.     
  519.     rawTE=GetWindowData_hTE(theData, kMainWindowTE);
  520.     InitTheParser(rawTE, 0, 32767);
  521.     parseError=ParseLoop();
  522.     if (parseError!=kNoError)
  523.     {
  524. // put error reporting here
  525.     }
  526.     
  527.     TESelView(rawTE);
  528.     AdjustVScrollBar(gVScrollBar, rawTE);
  529.     
  530.     gInProgress=FALSE;
  531.     AdjustMenus();
  532.     DrawMenuBar();
  533.     
  534.     return parseError;
  535. }
  536.